package com.ruoyi.school.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SendCodeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.school.domain.School;
import com.ruoyi.school.service.ISchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 学校管理Controller
 *
 * @author ruoyi
 * @date 2024-10-17
 */
@RestController
@RequestMapping("/school/school")
public class SchoolController extends BaseController {
    @Autowired
    private ISchoolService schoolService;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询学校管理列表
     */
    @PreAuthorize("@ss.hasPermi('school:school:list')")
    @GetMapping("/list")
    public TableDataInfo list(School school) {
        startPage();
        List<School> list = schoolService.selectSchoolList(school);
        return getDataTable(list);
    }

    /**
     * 导出学校管理列表
     */
    @PreAuthorize("@ss.hasPermi('school:school:export')")
    @Log(title = "学校管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, School school) {
        List<School> list = schoolService.selectSchoolList(school);
        ExcelUtil<School> util = new ExcelUtil<School>(School.class);
        util.exportExcel(response, list, "学校管理数据");
    }

    /**
     * 获取学校管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('school:school:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(schoolService.selectSchoolById(id));
    }

    /**
     * 新增学校管理
     */
    @PreAuthorize("@ss.hasPermi('school:school:add')")
    @Log(title = "学校管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody School school) {
        return toAjax(schoolService.insertSchool(school));
    }

    /**
     * 修改学校管理
     */
    @PreAuthorize("@ss.hasPermi('school:school:edit')")
    @Log(title = "学校管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody School school) {
        return toAjax(schoolService.updateSchool(school));
    }

    /**
     * 删除学校管理
     */
    @PreAuthorize("@ss.hasPermi('school:school:remove')")
    @Log(title = "学校管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(schoolService.deleteSchoolByIds(ids));
    }

    /**
     * 下载模版
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<School> util = new ExcelUtil<School>(School.class);
        util.importTemplateExcel(response, "学校数据");
    }

    /**
     * 按条件获取单个
     */
    @GetMapping("/one/{schoolName}")
    public AjaxResult getOne(@PathVariable String schoolName) {
        School school = new School();
        school.setSchoolName(schoolName);
        return success(schoolService.selectSchoolOne(school));
    }

    /**
     * 验证码
     */
    @PostMapping("/getPhoneCode")
    public AjaxResult getPhoneCode(@RequestParam String phoneNumber, @RequestParam String type) {
        String redisKey = phoneNumber + type;
        String code = SendCodeUtils.message(phoneNumber);
        redisCache.setCacheObject(redisKey, code, 5, TimeUnit.MINUTES);
        return AjaxResult.success("发送成功");
    }

    /**
     * 提交校验
     */
    @PostMapping("/verifyLegalPerson")
    public AjaxResult verifyLegalPerson(@RequestParam String phoneNumber, @RequestParam String type, @RequestParam String code) {
        String redisKey = phoneNumber + type;
        Object cachedObject = redisCache.getCacheObject(redisKey);
        if (code.equals(cachedObject)) {
            return AjaxResult.success("校验成功");
        }
        return AjaxResult.error("校验失败");
    }
}
